package cn.wolfcode.productdemo;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 异步消息发送
 * @Author: 伍家钰
 * @Date: 2020/9/4 10:34
 * @Version: 1.0
 */
@RestController
public class AsynchronousProductController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("send")
    public String sendMsg(String msg) {
        for (int i = 0; i < 100; i++) {
            rocketMQTemplate.asyncSend("02-boot-hello", msg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println(sendResult.getMessageQueue());
                    System.out.println(sendResult.getMsgId());
                    System.out.println("发送成功");
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                    System.out.println("信息发送失败");
                }
            });
        }
        return "success";
    }
}
